package com.kalix.ksh.record.biz;

import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ksh.record.api.biz.IWarningInfRecordBeanService;
import com.kalix.ksh.record.api.dao.IWarningInfRecordBeanDao;
import com.kalix.ksh.record.api.model.HttpRequestUtil;
import com.kalix.ksh.record.api.model.RequestTimeUtil;
import com.kalix.ksh.record.entities.WarningInfRecordBean;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DecimalFormat;


/**
 *
 * 功能描述: 根据告警信息扇形图需要数据格式，返回数据
 *
 * @auther: Whw
 * @date: 2019/1/14 0014 17:10
 * @param:
 * @return:
 */


public class WarningInfRecordServiceImpl extends GenericBizServiceImpl<IWarningInfRecordBeanDao, WarningInfRecordBean> implements IWarningInfRecordBeanService {
    public WarningInfRecordServiceImpl(){
        super.init(WarningInfRecordBean.class.getName());
    }
    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");
    public static String sTime = RequestTimeUtil.getCurrentTime();
    public static String lTime = RequestTimeUtil.getPastTime();
    public String  warningInf() {

        String url = "http://"+wsIp+":"+wsPort+"/query/mo/AlarmEvent?where=(alarmStatus=2%20or%20alarmStatus=3)&order=alarmTime%20desc";
        JSONArray  data1= HttpRequestUtil.getXpath(url);
        int a = data1.length();
        int b = getPolice();
        int num = a + b;
        double num1 = num ;
        double b1 = b ;
        int unhandlenum = num - b ;
        double p = b1/num1;
        double p1 = p*100;

        DecimalFormat    df   = new DecimalFormat("######0.00");
        String p2 = df.format(p1);



        JSONObject l= new JSONObject();
        l.put("0","value");
        l.put("1","name");
        l.put("2","label");
        l.put("3","normal");
        l.put("4","show");
        l.put("5","itemStyle");
        l.put("6","color");
        l.put("7","false");
        l.put("8","labelLine");

        String smallCircle1 = "[{'"+l.get(""+0)+"':"+unhandlenum+",'"+l.get(""+1)+"':'未处理数'},";
        String smallCircle2 ="{'"+l.get(""+0)+"':"+b+",'"+l.get(""+2)+"':{'"+l.get(""+3)+"':{'"+l.get(""+4)+"':"+l.get(""+7)+"}},'"+l.get(""+8)+"':{'"+l.get(""+3)+"':{'"+l.get(""+4)+"':"+l.get(""+7)+"}},";
        String smallCircle3 = "'"+l.get(""+5)+"':{'"+l.get(""+3)+"':{'"+l.get(""+6)+"':'rgba(0,0,0,0)'}}}]";
        String smallCircle = "{'smallCircle':"+smallCircle1+""+smallCircle2+""+smallCircle3+"},";


        String bigCircle1 = "[{'"+l.get(""+0)+"':"+unhandlenum+",'"+l.get(""+5)+"':{'"+l.get(""+3)+"':{'"+l.get(""+6)+"':'#3A6DEA'}},";
        String bigCircle2 = "'"+l.get(""+2)+"':{'"+l.get(""+3)+"':{'"+l.get(""+4)+"':"+l.get(""+7)+"}},";
        String bigCircle3 ="'"+l.get(""+8)+"':{'"+l.get(""+3)+"':{'"+l.get(""+4)+"':"+l.get(""+7)+"}}},";
        String bigCircle4 ="{'"+l.get(""+0)+"':"+b+",'"+l.get(""+1)+"':'已处理数','"+l.get(""+5)+"':{'"+l.get(""+3)+"':{'"+l.get(""+6)+"':'#39DEE6'}}}]";
        String bigCircle = "{'bigCircle':"+bigCircle1+""+bigCircle2+""+bigCircle3+""+bigCircle4+"},";


        String barName = " {'barName': ['未处理数(个)','已处理数(个)','设备处理占比(%)']},";
        String barValue = "{'barValue': [ "+unhandlenum+", "+b+","+p2+"] },";
        String title = "{'title1': '警告信息','title2':'处理率\\n"+p2+"%'}";

        String str = "["+smallCircle+""+bigCircle+""+barName+""+barValue+""+title+"]";


        return str;
    }

    public static int  getPolice(){
        String url = "http://"+wsIp+":"+wsPort+"/query/mo/AlarmEvent?where=(alarmStatus=4)and(alarmTime%20between%20%27"+lTime+"%27%20and%20%27"+sTime+"%27)";
        JSONArray  data2= HttpRequestUtil.getXpath(url);
        int b = data2.length();
        return b;
    }
}
